
var prefix = "/userAunt";
$(function() {
	let str = '';
	AjaxGet(prefix+'/seleProject').then(function (response){
		str += `<option value="">请选择</option>`;
		response.data.map(function (v){
			str += `<option value="${v.id}">${v.name}</option>`
		});
		$("#type").html(str)
	}).catch(function (error){
		layer.msg(error.msg)
	})
	load();
});

var pages = 1;
function load() {
	layui.use(['table', 'dropdown','form'], function(){
		var table = layui.table,
			form = layui.form ,
			$ = layui.$;

		// 创建渲染实例
		table.render({
			elem: '#exampleTable'
			,id:'exampleTable'
			,method:"POST"
			,contentType: 'application/json'
			// prefix
			,url: prefix + "/getUserAuntPage" // 此处为静态模拟数据，实际使用时需换成真实接口
			,cellMinWidth: 80
			, page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
				layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
				, curr: pages //设定初始在第 5 页
				, groups: 5 //只显示 1 个连续页码
				, limit : 10
			}
			,request:{
				pageName: 'current',
				limitName: 'pageSize'
			}
			,where: {
				name:$("#name").val().trim(),
				orderPhone:$("#orderPhone").val().trim(),
				age:$("#age").val().trim(),
				education:$("#education").val().trim(),
				projectId: $("#type").val(),
				signingStatus:$("#signingStatus").val(),
				enableStatus:$("#enableStatus").val(),
				auditStatus:1,
				auditResult:0
			}
			,defaultToolbar: false
			,loading: true
			,cols: [[
				{
					field : 'name',
					title : '姓名',
					width:100,
					align : 'center'
				},
				{
					field: '',
					title: '类型',
					align: 'center',
					// width:240,
					templet: function (d) {
						let str = '';
						let projectName = d.projectName;
						if ( projectName != null ){
							projectName = projectName.split('、');
							projectName.map(function (v,k){
								if( v != '' ){
									if ( k <= 2) {
										str += v+'、';
									}
								}
							})
							str=str.substring(0,str.length-1)
							return `
							<span>${str}</span>
							`;
						}else{
							return '-';
						}
					}
				},
				{
					field : 'signingStatus',
					title : '当前状态',
					align : 'center',
					width:100,
					templet : function( d ) {
						if (d.signingStatus == 1) {
							return '<span class="label label-primary">已上户</span>';
						} else if (d.signingStatus == 0) {
							return '<span class="label label-warning">待上户</span>';
						}
					}
				},
				{
					field : 'orderPhone',
					title : '手机号',
					width:100,
					align : 'center'
				},
				{
					field : 'education',
					title : '学历',
					width:100,
					align : 'center'
				},
				{
					field : 'age',
					title : '年龄',
					width : 60,
					align : 'center'
				},
				{
					field : 'orderCount',
					title : '完成总订单数',
					width:100,
					align : 'center'
				},
				{
					field : 'startTime',
					title : '工作开始时间',
					width:100,
					align : 'center'
				},
				{
					field : 'endTime',
					title : '工作结束时间',
					width:100,
					align : 'center'
				},
				{
					field : 'createTime',
					title : '加入时间',
					width:200,
					align : 'center'
				},
				{
					field : 'checkbox',
					title : '是否启用',
					align : 'center',
					width:40,
					templet:(d)=>{
						console.log('enableStatus',d.enableStatus)
						return upAndDownPrices?((d.enableStatus=='1'?` 
      <input type="checkbox" checked `:'<input type="checkbox" ')+` name="open"  value="${d.id}"
      title="开|关" myid="${d.id}" lay-skin="switch" lay-filter="switchTest" title="是否启用"> 
						`):(d.enableStatus=='1'?'<span class="label label-primary">上架</span>':'<span class="label label-warning">下架</span>')
					}
				},
				{
					title : '操作',
					field : 'id',
					align : 'center',
					width:160,
					templet : function(d) {
						var e = '<a  class="btn btn-primary btn-sm '+s_info_h+' " href="#" mce_href="#" title="详情" onclick="lookInfo(\''
							+ d.id
							+ '\')"><i class="fa fa-send"></i></a> ';

						var b = '<a  class="btn btn-primary btn-sm '+s_edit_h+' " href="#" mce_href="#" title="编辑" onclick="edit(\''
							+ d.id + '\', \'' + d.sysUserid
							+ '\')"><i class="fa fa-edit"></i></a> ';

						var ss = '<a class="btn btn-success btn-sm '+s_remove_h+' " href="#" title="删除"  mce_href="#" onclick="remove(\''
							+ d.sysUserid
							+ '\')"><i class="fa fa-remove"></i></a>';
						return e + b + ss;
					}
				}
			]]
			,response: {
				statusCode: 0 //重新规定成功的状态码为 200，table 组件默认为 0
			}
			,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
				if( res.code != 0 ){

					return false;
				}

				// let screen = [];
				// res.data.records.forEach(function (v,k){
				// 	if( v.signingStatus != 0 ){
				// 		screen.push(v);
				// 	}
				// })

				return {
					"code": res.code, //解析接口状态
					"msg": res.msg, //解析提示文本
					"count": res.data.total, //解析数据长度
					"data": res.data.records //解析数据列表
				};
			}
			,jump: function (obj, first) {
				//首次不执行
				if (!first) {
						pages = obj.curr
				}
			}
			,error: function(res, msg){

			}

			,done: function (res){
				form.render()
			}
		});

		var $ = layui.$, active = {
			reload: function(){
				//执行重载
				table.reload('exampleTable', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
					,where: {
						name:$("#name").val().trim(),
						age:$("#age").val().trim(),
						education:$("#education").val().trim(),
						orderPhone:$("#orderPhone").val().trim(),
						projectId: $("#type").val(),
						signingStatus:$("#signingStatus").val(),
						enableStatus:$("#enableStatus").val(),
						auditStatus:1,
						auditResult:0
					}
				});
			}
		};
		form.on('switch(switchTest)', function(data){
			let id=$(data.othis[0].parentNode).find('input').attr('myid')
			var loadIndex = layer.load(0);
			AjaxPost('/userAunt/updateUserAnutStatus', {id,enableStatus:this.checked?'1':'0'}).then(function (response){
				parent.layer.msg("操作成功");
				layer.close(loadIndex)
				load();
			}).catch(error => {
				layer.msg(error.msg)
				layer.close(loadIndex)
				load();
			})




		})
		$('.demoTable .layui-btn').on('click', function(){
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

		//触发表格复选框选择
		table.on('checkbox(test)', function(obj){
			console.log(obj)
		});

		//触发表格单选框选择
		table.on('radio(test)', function(obj){
			console.log(obj)
		});

	});

}

$("#add").click(function (){

	let toIndex=layer.open({
		type: 2,
		title:'添加月嫂',
		maxmin: true,
		shadeClose: false,
		area:['800px','500px'],
		content: '/userAunt/add'
	});
	layer.full(toIndex);
})

function edit(id,sysUserid){
	sessionStorage.setItem('matronId',JSON.stringify({id:id,type:1}));
	let toIndex=layer.open({
		type: 2,
		title:'月嫂修改',
		maxmin: true,
		shadeClose: false,
		area:['800px','500px'],
		content:  `/userAunt/edit/${id}/${sysUserid}/aduitEdit`
	});
	layer.full(toIndex);
}

function lookInfo(id){
	sessionStorage.setItem('matronId',id);
	let toIndex= layer.open({
		type: 2,
		title:'月嫂详情',
		maxmin: true,
		shadeClose: false,
		area:['800px','500px'],
		content:  `/userAunt/info/${id}/infor`
	});
	layer.full(toIndex);
}

function remove(id) {

	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		$.ajax({
			url : '/userManagement/deletedByUserId',
			type : "post",
			data : {
				'id' : id
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					load();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}